import pandas as pd
import plotly.express as px
import plotly.graph_objects as go


def draw_one_factor_pic(
    factor_values: dict[tuple[str, str], list[float]],
    factor: str = None,
    symbol: str = None,
):
    if len(factor_values) == 0:
        return None
    if factor:
        df = pd.DataFrame(
            {
                xsymbol: factor_values[(xsymbol, factor)]
                for xsymbol, xfactor in factor_values.keys()
                if xfactor == factor
            }
        )
        if len(df) <= 0:
            return None
        fig = px.line(
            df,
            title=f"{factor} Factor Curve",
            labels={
                "index": "Time",
                "value": "Value",
                "variable": "Symbol",
            },
        )
        return fig

    if symbol:
        df = pd.DataFrame(
            {
                xfactor: factor_values[(xsymbol, xfactor)]
                for xsymbol, xfactor in factor_values.keys()
                if xsymbol == symbol
            }
        )
        if len(df) <= 0:
            return None
        fig = px.line(
            df,
            title=f"{symbol} Symbol Curve",
            labels={
                "index": "Time",
                "value": "Value",
                "variable": "Factor",
            },
        )
        return fig


def draw_kline(df_kline: pd.DataFrame, symbol: str):
    fig = go.Figure(
        data=[
            go.Candlestick(
                x=df_kline.index,
                open=df_kline["open"],
                high=df_kline["high"],
                low=df_kline["low"],
                close=df_kline["close"],
            )
        ]
    )
    fig.update_layout(
        title=f"{symbol} K-line Chart",
        xaxis_title="Time",
        yaxis_title="Price",
        xaxis_rangeslider_visible=False,
    )
    return fig
